﻿using System.Data;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Twitter.Models;

namespace Twitter
{
    public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            SchemaBuilder.CreateTable("TwitterSettingsPartRecord", table => table
                .ContentPartRecord()
                .Column("Username", DbType.String)
               );

            SchemaBuilder.CreateTable("ProfilePartRecord", table => table
                .ContentPartRecord()
                .Column("Username", DbType.String)
                .Column("Width", DbType.Int32)
                .Column("Height", DbType.Int32)
                .Column("BackgroundShellColor", DbType.String)
                .Column("ShellColor", DbType.String)
                .Column("TweetsBackgroundColor", DbType.String)
                .Column("TweetsColor", DbType.String)
                .Column("TweetsLinksColor", DbType.String)
                .Column("NumberOfTweets", DbType.Int32)
                .Column("ScrollBar", DbType.Boolean)
                .Column("Loop", DbType.Boolean)
                .Column("Live", DbType.Boolean)
                .Column("HashTags", DbType.Boolean)
                .Column("Timestamp", DbType.Boolean)
                .Column("Avatars", DbType.Boolean)
                .Column("Behavior", DbType.Int32)
                .Column("Interval", DbType.Int32)
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(ProfilePart).Name, cfg => cfg
                .Attachable());

            ContentDefinitionManager.AlterTypeDefinition("TwitterProfileWidget", cfg => cfg
                .WithPart("ProfilePart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            SchemaBuilder.CreateTable("TweetButtonPartRecord", table => table
                .ContentPartRecord()
                .Column("Username", DbType.String)
                .Column("RelatedUsername", DbType.String)
                .Column("RelatedUserDescription", DbType.String)
                .Column("TweetText", DbType.String)
                .Column("Url", DbType.String)
                .Column("DataCount", DbType.Int32)
                .Column("Language", DbType.Int32)
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(TweetButtonPart).Name, cfg => cfg
                .Attachable());

            ContentDefinitionManager.AlterTypeDefinition("TweetButtonWidget", cfg => cfg
                .WithPart("TweetButtonPart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            SchemaBuilder.CreateTable("SearchPartRecord", table => table
                .ContentPartRecord()
                .Column("SearchQuery", DbType.String)
                .Column("Width", DbType.Int32)
                .Column("Height", DbType.Int32)
                .Column("BackgroundShellColor", DbType.String)
                .Column("ShellColor", DbType.String)
                .Column("TweetsBackgroundColor", DbType.String)
                .Column("TweetsColor", DbType.String)
                .Column("TweetsLinksColor", DbType.String)
                .Column("ScrollBar", DbType.Boolean)
                .Column("Loop", DbType.Boolean)
                .Column("Live", DbType.Boolean)
                .Column("HashTags", DbType.Boolean)
                .Column("Timestamp", DbType.Boolean)
                .Column("Avatars", DbType.Boolean)
                .Column("TopTweets", DbType.Boolean) //, column => column.NotNull().WithDefault(true)
                .Column("Behavior", DbType.Int32)
                .Column("Interval", DbType.Int32)
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(SearchPart).Name, cfg => cfg
                .Attachable());

            ContentDefinitionManager.AlterTypeDefinition("TwitterSearchWidget", cfg => cfg
                .WithPart("SearchPart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            return 1;
        }

        public int UpdateFrom1()
        {
            SchemaBuilder.AlterTable("SearchPartRecord", table => table
                .AddColumn("WidgetTitle", DbType.String)
            );
            SchemaBuilder.AlterTable("SearchPartRecord", table => table
                .AddColumn("Caption", DbType.String)
            );

            SchemaBuilder.CreateTable("FavesPartRecord", table => table
                .ContentPartRecord()
                .Column("Username", DbType.String)
                .Column("Width", DbType.Int32)
                .Column("Height", DbType.Int32)
                .Column("WidgetTitle", DbType.String)
                .Column("Caption", DbType.String)
                .Column("BackgroundShellColor", DbType.String)
                .Column("ShellColor", DbType.String)
                .Column("TweetsBackgroundColor", DbType.String)
                .Column("TweetsColor", DbType.String)
                .Column("TweetsLinksColor", DbType.String)
                .Column("NumberOfTweets", DbType.Int32)
                .Column("ScrollBar", DbType.Boolean)
                .Column("Loop", DbType.Boolean)
                .Column("Live", DbType.Boolean)
                .Column("HashTags", DbType.Boolean)
                .Column("Timestamp", DbType.Boolean)
                .Column("Avatars", DbType.Boolean)
                .Column("Behavior", DbType.Int32)
                .Column("Interval", DbType.Int32)
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(FavesPart).Name, cfg => cfg
                .Attachable());

            ContentDefinitionManager.AlterTypeDefinition("TwitterFavesWidget", cfg => cfg
                .WithPart("FavesPart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            SchemaBuilder.CreateTable("ListPartRecord", table => table
                .ContentPartRecord()
                .Column("Username", DbType.String)
                .Column("List", DbType.String)
                .Column("Width", DbType.Int32)
                .Column("Height", DbType.Int32)
                .Column("WidgetTitle", DbType.String)
                .Column("Caption", DbType.String)
                .Column("BackgroundShellColor", DbType.String)
                .Column("ShellColor", DbType.String)
                .Column("TweetsBackgroundColor", DbType.String)
                .Column("TweetsColor", DbType.String)
                .Column("TweetsLinksColor", DbType.String)
                .Column("NumberOfTweets", DbType.Int32)
                .Column("ScrollBar", DbType.Boolean)
                .Column("Loop", DbType.Boolean)
                .Column("Live", DbType.Boolean)
                .Column("HashTags", DbType.Boolean)
                .Column("Timestamp", DbType.Boolean)
                .Column("Avatars", DbType.Boolean)
                .Column("Behavior", DbType.Int32)
                .Column("Interval", DbType.Int32)
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(ListPart).Name, cfg => cfg
                .Attachable());

            ContentDefinitionManager.AlterTypeDefinition("TwitterListWidget", cfg => cfg
                .WithPart("ListPart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            return 2;
        }
    }
}